package org.beyondhc.lessons.observer;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import junit.framework.TestCase;

import org.junit.Test;
import org.mockito.Mockito;

public class ActionListenerExample extends TestCase {

    public interface View {

        void showHelpWanted();

    }

    public class Menus {
        private final View view;

        public Menus(final View view) {
            this.view = view;
        }

        public JMenuBar createMenus() {
            final JMenu helpMenu = new JMenu("Help");
            final JMenuItem helpMenuItem = new JMenuItem();
            final Action helpAction = new AbstractAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(final ActionEvent e) {
                    view.showHelpWanted();
                }
            };
            helpMenuItem.setAction(helpAction);
            // setting action resets menu text, image and mnemonics
            helpMenuItem.setText("Help Contents");
            helpMenuItem.setMnemonic('H');
            final int noModifiers = 0;
            helpMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,
                    noModifiers));

            helpMenu.add(helpMenuItem);
            System.out.println(helpMenuItem.getActionCommand());
            final JMenuBar menuBar = new JMenuBar();
            menuBar.add(helpMenu);
            return menuBar;
        }

    }

    @Test
    public void test() throws InterruptedException {
        final View view = Mockito.mock(View.class);
        final Menus menus = new Menus(view);
        final JFrame frame = new JFrame();
        final JMenuBar menuBar = menus.createMenus();
        frame.setJMenuBar(menuBar);
        frame.setSize(300, 300);
        frame.setVisible(true);
        // Thread.sleep(30000);
        // TODO: fail("Not yet implemented");
    }

}
